//
//  CCLoginViewController.m
//  Activity
//
//  Created by coco on 15/5/8.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCLoginViewController.h"
#import "CCLoginViewModel.h"
#import "CCError.h"
@interface CCLoginViewController ()
@property (nonatomic, strong) CCLoginViewModel *viewModel;
@property (nonatomic, strong) UITextField *user;
@property (nonatomic, strong) UITextField *password;
@property (nonatomic, strong) UIImageView *userLeft;
@property (nonatomic, strong) UIImageView *passwordLeft;
@property (nonatomic, strong) UIButton *forgetPassword;
@property (nonatomic, strong) UIButton *registerUser;
@property (nonatomic, strong) UIButton *next;
@end

@implementation CCLoginViewController

- (id) initWithViewModel:(CCLoginViewModel *)viewModel {
  if (self = [super init]) {
    _viewModel = viewModel;
  }
  return self;
}

- (void)viewDidLoad {
  [self setupUI];
  [self setupCSS];
  [self bindUI];
  [super viewDidLoad];
}

- (void) setupUI {
  self.view.backgroundColor = RGB(236, 237, 236, 1);
  _user = [[UITextField alloc] init];
  _user.backgroundColor = [UIColor whiteColor];
  _user.leftViewMode = UITextFieldViewModeAlways;
  _user.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 4, 1)];
  _user.layer.cornerRadius = 5.0f;
  _user.layer.borderWidth = 0.1f;
  _user.placeholder = @"请输入手机号/邮箱";
  _password = [[UITextField alloc] init];
  _password.leftViewMode = UITextFieldViewModeAlways;
  _password.layer.cornerRadius = 5.0f;
  _password.layer.borderWidth = 0.1f;
  _password.placeholder = @"请输入密码";
  _password.backgroundColor = [UIColor whiteColor];
  _password.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 4, 1)];
  _password.secureTextEntry = YES;
  _password.returnKeyType = UIReturnKeyGo;
  _next = [UIButton buttonWithType:UIButtonTypeCustom];
  [_next setTitle:@"下一步" forState:UIControlStateNormal];
  [_next setTitleColor:RGB(255, 255, 255, 1) forState:UIControlStateNormal];
  [_next setBackgroundImage:IMAGE(@"btn_normal") forState:UIControlStateNormal];
  

  _forgetPassword = [UIButton buttonWithType:UIButtonTypeCustom];
  [_forgetPassword setTitle:@"忘记密码" forState:UIControlStateNormal];
  [_forgetPassword setTitleColor:RGB(51, 145, 94, 1) forState:UIControlStateNormal];
  
  _registerUser = [UIButton buttonWithType:UIButtonTypeCustom];
  [_registerUser setTitle:@"注册账号" forState:UIControlStateNormal];
  [_registerUser setTitleColor:RGB(51, 145, 94, 1) forState:UIControlStateNormal];

  [self.view addSubview:_user];
  [self.view addSubview: _password];
  [self.view addSubview:_next];
  [self.view addSubview: _forgetPassword];
  [self.view addSubview:_registerUser];
  
}

- (void) setupCSS {
  [_user mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view).offset(20);
    make.top.equalTo(self.view).offset(20+self.yoffset);
    make.right.equalTo(self.view).offset(-20);
    make.height.mas_equalTo(40);
  }];
  [_password mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view).offset(20);
    make.top.equalTo(self.user.mas_bottom).offset(5);
    make.right.equalTo(self.view).offset(-20);
    make.height.mas_equalTo(40);
  }];
  
  [_next mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view).offset(20);
    make.top.equalTo(_password.mas_bottom).offset(20);
    make.right.equalTo(self.view).offset(-20);
    make.height.mas_equalTo(40);
  }];

  [_forgetPassword mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view).offset(20);
    make.top.equalTo(self.next.mas_bottom).offset(20);
    make.size.mas_equalTo(CGSizeMake(80, 23));
  }];
  
  [_registerUser mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.view).offset(-20);
    make.top.equalTo(self.next.mas_bottom).offset(20);
    make.size.mas_equalTo(CGSizeMake(80, 23));
  }];
  
}

- (void) bindUI {
  self.next.rac_command = [[RACCommand alloc] initWithEnabled:[RACSignal combineLatest:@[self.user.rac_textSignal, self.password.rac_textSignal] reduce:^id(NSString *user, NSString *password){
    return @(user.length > 0 && password.length > 0);
  }]
                                                  signalBlock:^RACSignal *(id input) {
                                                    [CCProgressHUD showHUBInView:self.view withTimeOut:0 str:nil];
                                                     [[self.viewModel login] subscribeNext:^(CCError *x) {
                                                       if (x.code != 5000) {
                                                         [CCProgressHUD showMessage:self.view withTimeOut:2.0f str:x.des];
                                                       } else {
                                                         [self dismissViewControllerAnimated:YES completion:^{
                                                           
                                                         }];
                                                       }
                                                     }];
                                                    return [RACSignal empty];
                                                  }];
  
  RAC(self, title) = RACObserve(self.viewModel, title);
  RAC(self.viewModel, user) = self.user.rac_textSignal;
  RAC(self.viewModel, password) = self.password.rac_textSignal;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
